package com.aerfazhe.config;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * @Author：wk
 * @Slogan：无论风雨，和自己一决胜负吧
 * @Create：2022/7/3/8:55
 * @Description 跨域配置 和yaml配置任选其一即可
 * 使用代码跨域配置将 @Component和@Bean注释去掉，跨域yaml配置加上注释即可，同理相反
 * @Version：1.0
 */
@Component
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.addAllowedHeader("*"); //允许访问的请求头
        configuration.addAllowedMethod("*"); //允许访问的请求方式
        configuration.addAllowedOrigin("*"); //允许的请求来源

//        允许访问的资源
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**",configuration);
        return new CorsWebFilter(source);
    }
}
